package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.service.SpecialGroupService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 特别分组管理
 */
@Controller
@RequestMapping(value = "backend/specialGroup")
public class SpecialGroupController extends CommonController {

    public static Map<String, Map<String, Object>> tempSpecialGroupMap = new HashMap<>();

    @Autowired
    SpecialGroupService specialGroupService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/specialGroup/list";
    }

    @RequestMapping(value = "/child/index")
    public String childIndex(Model model, Integer id, Integer pageNum) {
        model.addAttribute("specialGroup", tempSpecialGroupMap.get(id + ""));
        model.addAttribute("pageNum", pageNum);

        return "backend/specialGroup/childList";
    }

    @RequestMapping(value = "/child/user/index")
    public String childUserIndex(Model model, Integer id, Integer pageNum) {
        model.addAttribute("specialGroup", tempSpecialGroupMap.get(id + ""));
        model.addAttribute("pageNum", pageNum);

        return "backend/specialGroup/userList";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, specialGroupService.pageByParams(keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> saveInfo(HttpServletRequest request, Integer companyId, String content, Integer turnIndex) {
        return specialGroupService.saveInfo(request, companyId, content, turnIndex);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> delete(HttpServletRequest request, Integer id) {
        return specialGroupService.delInfo(request, id);
    }

    @RequestMapping("/child/list")
    public void childList(HttpServletResponse response, Integer specialGroupId, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, specialGroupService.childPageByParams(specialGroupId, keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/child/save")
    @ResponseBody
    public Map<String, Object> saveChildInfo(HttpServletRequest request, Integer specialGroupId, Integer deviceId) {
        return specialGroupService.saveChildInfo(request, specialGroupId, deviceId);
    }

    @RequestMapping("/child/del")
    @ResponseBody
    public Map<String, Object> childDelete(HttpServletRequest request, Integer id) {
        return specialGroupService.delChildInfo(request, id);
    }

    @RequestMapping("/child/user/list")
    public void childUserList(HttpServletResponse response, Integer specialGroupId, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, specialGroupService.childUserPageByParams(specialGroupId, keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/child/user/save")
    @ResponseBody
    public Map<String, Object> saveChildUserInfo(HttpServletRequest request, Integer specialGroupId, Integer userId) {
        return specialGroupService.saveChildUserInfo(request, specialGroupId, userId);
    }

    @RequestMapping("/child/user/del")
    @ResponseBody
    public Map<String, Object> childUserDelete(HttpServletRequest request, Integer id) {
        return specialGroupService.delChildUserInfo(request, id);
    }
}
